const app = getApp();
Page({

  data: {
    searchList: []
  },

  onLoad: function (options) {

  },
  getValue(event) {
    console.log(event.detail.value);
    let inputValue = event.detail.value;
    this.setData({
      inputValue
    })

  },
  search() {
    wx.request({
      url: `${app.globalData.baseurl}goods/`,
      method: 'GET',
      success: (res) => {
        if (res.statusCode == 200) {
          // 取出来对象的goodsname为数组内的一项
          let goodsList = [];
          let searchGoodList = [];
          let resultData = res.data;
          let resultData2 = [];
          console.log('res.data:', res.data);
          resultData.map((item) => {
            let obj = [];
            for (let i in item) {
              obj.push(JSON.stringify(item[i]))
            }
            // console.log(obj);
            resultData2.push(obj);
            // console.log(resultData2);
            return obj;
          }).join(',').split(',');
          // console.log('resultData2:' + resultData2);
          // 获取到搜索框内查找到的那一部分内容
          // console.log(resultData2.length);
          for (var i = 0; i < resultData2.length; i++) {
            let arr = resultData2[i];
            // console.log(arr);
            let list = arr.filter((e) => {
              // console.log(e);
              if (e.indexOf(this.data.inputValue) != -1) {
                return e
              }
            })
            searchGoodList.push(list);
            // console.log(searchGoodList);
            // resultData2
          }
          for(var i=0;i<searchGoodList.length;i++){
            if(searchGoodList[i].length!=0){
              goodsList.push(resultData[i]);
              console.log(goodsList);
            }
          }
          this.setData({
            searchList: goodsList
          });
          console.log(this.data.searchList);
        }
      },
      fail: (err) => {
        console.log('error!');
      }
    })
  },
  toGoodDetail(event){
    console.log(event.currentTarget.dataset.id);
    let id=event.currentTarget.dataset.id;
    wx.navigateTo({
      url:'../../goodDetail/goodDetail?goodsId='+id,
    })
  },
})

